#include "myBTree.h"

void Print_post(Node* root){
    if(NULL == root) return;
    Print_post(root->_left);
    Print_post(root->_right);
    printf("%c", root->_data);
}

Node *BuyNode(char data){
    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->_left = NULL;
    newNode->_right = NULL;
    newNode->_data = data;

}

Node *TreeGrow(const char *str, int* pos){
    char token = str[*pos];
    if(token == '#') 
    {   
        ++(*pos);
        return NULL;
    }
    Node *newNode = BuyNode(token);
    ++(*pos);
    //
    newNode->_left = TreeGrow(str, pos);
    newNode->_right = TreeGrow(str, pos);
    return newNode;
}

Node *Pre_In_MKTree(const char *pre, const char *in, int in_start, int in_end){
    static int pos = 0;
    if(!pre[pos] || !in[pos]) return NULL;
    if(in_start > in_end) return NULL;
    
    char root = pre[pos++];
    Node *newNode = BuyNode(root);
    //int diff = strchr(in, root) - in;
    int index = in_start;
    for(; index <= in_end; ++index){
        if(in[index] == root){ break; }
    }
    newNode->_left = Pre_In_MKTree(pre, in , in_start, index - 1);
    newNode->_right = Pre_In_MKTree(pre, in, index + 1, in_end);

    return newNode;
}
